package com.rao.rabbitmq.topic;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rao.rabbitmq.util.ConnectionUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 生产者，模拟为商品服务（rabbitMQ 路由适配模式）
 *
 * @author raos
 * @emil 991207823@qq.com
 * @date 2021/6/17 23:35
 */
public class Send {
    private static final Logger log = LoggerFactory.getLogger(Send.class);

    private static final String EXCHANGE_NAME = "topic_exchange_test";

    public static void main(String[] argv) throws Exception {
        // 获取到连接
        Connection connection = ConnectionUtil.getConnection();
        // 获取通道
        Channel channel = connection.createChannel();
        // 声明exchange，指定类型为topic
        channel.exchangeDeclare(EXCHANGE_NAME, "topic",true);
        // 消息内容
//        String message = "新增商品: id = 1001";
//        // 发送消息，并且指定routing key 为：insert-新增商品,update-修改商品,delete-删除商品
//        channel.basicPublish(EXCHANGE_NAME, "item.insert", null, message.getBytes());
//        channel.basicPublish(EXCHANGE_NAME, "item.insert", MessageProperties.PERSISTENT_TEXT_PLAIN, message.getBytes());
//        String message = "修改商品: id = 1001";
//        channel.basicPublish(EXCHANGE_NAME, "item.update", null, message.getBytes());
        String message = "删除商品: id = 1001";
        channel.basicPublish(EXCHANGE_NAME, "item.delete", null, message.getBytes());
        log.info("[商品服务] Sent【{}】", message);

        channel.close();
        connection.close();
    }
}